#include<stdio.h>
#include<string.h>                                                              
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>

#define SERVER_PORT 8000
#define SERVER_IP "127.0.0.1"

int main(int argc, char* argv[])
{
    int sfd = socket(AF_INET,SOCK_STREAM,0);

    struct sockaddr_in server_addr,client_addr;
    server_addr.sin_family = AF_INET;
    //本地字节序转为网络字节序host-to-network-short
    server_addr.sin_port = htons(SERVER_PORT);
    int dst;
    inet_pton(AF_INET,SERVER_IP,(void*)&dst);
    server_addr.sin_addr.s_addr = dst;  //整数，大端字节数

    // 绑定ip和端口号
    bind(sfd,(struct sockaddr*)&server_addr,sizeof(server_addr));

    //主动套接字转为被动套接字
    listen(sfd,64);

    //新创建出服务客户端的套接字
    socklen_t addrlen = sizeof(client_addr);
    int cfd = accept(sfd,(struct sockaddr*)&client_addr,&addrlen);
    printf("客户端建立连接\n");

    char buf[1024];
    // int read_count = read(cfd,buf,sizeof(buf));
    // write(STDOUT_FILENO,buf,read_count);

    int read_ret = read(cfd,buf,sizeof(buf));
    write(STDOUT_FILENO,buf,read_ret);
    while(1);

    return 0;
}


